package com.example.lynandroid

import android.annotation.SuppressLint
import android.content.res.Configuration
import android.os.Bundle
import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.add
import androidx.fragment.app.commit
import com.example.lynandroid.bean.DirectoryBean
import com.example.lynandroid.fragment.DirectoryFragment
import com.example.lynandroid.values.Sources
import kotlin.random.Random

class MainActivity : FragmentActivity(R.layout.activity) {
    private lateinit var tvParam: TextView

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val bundle = bundleOf(KEY_DIR to getDir())
        tvParam = findViewById<TextView>(R.id.param).apply {
            typeface = Sources.loveTtf
        }
        updateText()
        supportFragmentManager.commit {
            if (savedInstanceState != null) {
                return@commit
            }
            setReorderingAllowed(true)
            add<DirectoryFragment>(R.id.fragment, args = bundle)
        }
    }

    private fun getDir(): Array<DirectoryBean> {
        return arrayOf(
            DirectoryBean("最好的键盘切换", fragment = FRAGMENT_INPUT),
            DirectoryBean("九点图", fragment = FRAGMENT_PATCH9),
            DirectoryBean("Activity启动", img = IMG2, fragment = ACTIVITY_BOOT),
            DirectoryBean("WINDOW ACTIVITY VIEW", fragment = ACTIVITY_WINDOW),
            DirectoryBean("自定义View", img = IMG3, fragment = FRAGMENT_VIEW),
            DirectoryBean("研究Fragment", img = IMG4, fragment = FRAGMENT_WINDOW),
            DirectoryBean("研究EditText", fragment = FRAGMENT_EDIT)
        )
    }

    @SuppressLint("SetTextI18n")
    private fun updateText() {
        tvParam.text =
            "width=${screenWidth(this@MainActivity)},height=${screenHeight(this@MainActivity)},ratio=${
                screenHeight(this@MainActivity) / screenWidth(this@MainActivity).toFloat()
            }}"
    }

    override fun onResume() {
        super.onResume()
        updateText()
    }

    override fun onDestroy() {
        super.onDestroy()
    }


    @SuppressLint("SetTextI18n")
    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
    }
}